﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace TTBox
{
    public enum BoxStatusItem { GPRS, GPS, ACC, Server, Box };
    public partial class BoxStatus : UserControl
    {
        public BoxStatus()
        {
            InitializeComponent();
            this.label_ACC.BackColor = Color.FromArgb(StoreData.getInstance().BackColor);
            this.label_GPRS.BackColor = Color.FromArgb(StoreData.getInstance().BackColor);
            this.label_GPS.BackColor = Color.FromArgb(StoreData.getInstance().BackColor);
            this.label_Server.BackColor = Color.FromArgb(StoreData.getInstance().BackColor);

            this.label_ACC.ForeColor = Color.FromArgb(StoreData.getInstance().ForeColor);
            this.label_GPRS.ForeColor = Color.FromArgb(StoreData.getInstance().ForeColor);
            this.label_GPS.ForeColor = Color.FromArgb(StoreData.getInstance().ForeColor);
            this.label_Server.ForeColor = Color.FromArgb(StoreData.getInstance().ForeColor);

            this.panel_Status.BackColor = this.label_GPRS.BackColor;
            this.timer_Checker.Enabled = true;
            m_bACC = true;

        }


        bool m_bGPS;
        bool m_bGPRS;
        bool m_bACC;
        bool m_bServer;
        bool m_bBoxConn;


        public bool GPSStatus { get { return m_bGPS; } }
        public bool GPRSStatus { get { return m_bGPRS; } }
        public bool ACCStatus { get { return m_bACC; } }
        public bool ServerStatus { get { return m_bServer; } }
        public bool BoxConn { get { return m_bBoxConn; } }


        private DateTime dtBoxConn = System.DateTime.Now.Add(new TimeSpan(0, 0, 8));
        private DateTime dtServer = System.DateTime.Now.Add(new TimeSpan(0, 5, 30));

        private void resetdtServer()
        {
            dtServer = System.DateTime.Now.Add(new TimeSpan(0, 5, 30));
        }

        private void resetdtBoxConn()
        {
            dtBoxConn = System.DateTime.Now.Add(new TimeSpan(0, 0, 8));
        }


        public void setGPSStatus(bool arg_bGPS)
        {
            //if (!m_bGPS && arg_bGPS)
            {
                //收斂後, 立刻送一次
               // CmdTalker.getInstance().SendToServerNoWaitACK();
            }
            m_bGPS = arg_bGPS;
            updateStatus();
        }
        public void setGPRSStatus(bool arg_bGPRS)
        {
            if (m_bGPRS == false && arg_bGPRS == true)
            {
                Form_Main.Main.Screen_MainScreen.sendCurrStatus();
            }
            m_bGPRS = arg_bGPRS;
            updateStatus();
        }
        public void setACCStatus(bool arg_bAcc)
        {
            m_bACC = arg_bAcc;
            m_bACC = true;
            updateStatus();
        }
        public void setServerStatus(bool arg_bServer)
        {
            resetdtServer();
            m_bServer = arg_bServer;
            updateStatus();
        }

        public void setBoxConn(bool arg_bBox)
        {
            resetdtBoxConn();
            m_bBoxConn = arg_bBox;
            updateStatus();
        }

        public void updateStatus()
        {

            try
            {
                this.label_GPS.Visible = !m_bGPS;
                this.label_Server.Visible = !m_bServer;
                this.label_GPRS.Visible = !m_bGPRS;
                this.label_ACC.Visible = !m_bACC;
            }
            catch (Exception ex)
            {
            }
        }

        private void timer_Checker_Tick(object sender, EventArgs e)
        {
            try
            {
                if (dtBoxConn < System.DateTime.Now)
                {
                    //setBoxConn(false);
                    //setServerStatus(false);

                    resetdtBoxConn();
                }
                
                if (dtServer < System.DateTime.Now)
                {
                    setServerStatus(false);
                    resetdtServer();
                }
                 
            }
            catch (Exception ex)
            {
                LogManager.getInstance().Log("Error", ex);
            }
        }
    }
}
